
package mx.unam.fciencias.ppc.beansboard.comm;

import java.io.File;

/**
 * Class for the messaging protocol between peers. Designed to be extended by the client and server
 * @author lander
 */
public abstract class CollabService {

    protected static final String DELIMITER = ";";
    protected static final int LOCK         = 0;
    protected static final int UNLOCK       = 1;
    protected static final int SEND         = 2;
    protected static final int DELETE       = 3;
    protected static final int INSERT       = 4;
    protected static final int LOGIN        = 5;
    protected static final int LOGOUT       = 6;
    protected static final int CREATEFILE   = 7;
    protected static final int DELETEFILE   = 8;
    protected static final int CHECKOUT     = 9;
    protected static final int SENDFILE     = 10;
    protected static final int GETFILE      = 11;
    
    
    
    
    protected final String lock(String user, String file, int line) {
        return LOCK + CollabService.DELIMITER + user + CollabService.DELIMITER + file + CollabService.DELIMITER + line;
    }

    protected final String unlock(String user, String file, int line) {
        return UNLOCK + CollabService.DELIMITER + user + CollabService.DELIMITER + file + CollabService.DELIMITER + line;
    }

    protected final String send(String user, String file, int line, String content) {
        return SEND + CollabService.DELIMITER + user + CollabService.DELIMITER + file + CollabService.DELIMITER + line + CollabService.DELIMITER + content;
    }

    protected final String delete(String user, String file, int line) {
        return DELETE + CollabService.DELIMITER + user + CollabService.DELIMITER + file + CollabService.DELIMITER + line;
    }
    
    protected final String insert(String user, String file, int line) {
        return INSERT + CollabService.DELIMITER + user + CollabService.DELIMITER + file + CollabService.DELIMITER + line;
    }
    
    protected final String login(String user) {
        return LOGIN + CollabService.DELIMITER + user;
    }
    
    protected final String logout(String user) {
        return LOGOUT + CollabService.DELIMITER + user;
    }
    
    protected final String createFile(String user, File file) {
        return CREATEFILE + CollabService.DELIMITER + user + CollabService.DELIMITER + file;
    }
    
    protected final String deleteFile(String user, File file) {
        return DELETEFILE + CollabService.DELIMITER + user + CollabService.DELIMITER + file;
    }
    
    protected final String checkOut(File[] file) {
        String files = "";
        for(int i = 0; i < file.length; ++i){
            files += file[i].toString()+CollabService.DELIMITER;
        }
        
        return CHECKOUT + CollabService.DELIMITER + files.substring(0,files.length()-1);
    }
    
    protected final String sendFile(File file){
        return SENDFILE + CollabService.DELIMITER + file.length() + CollabService.DELIMITER + file;
    }
    
    protected final String getFile(File file){
        return GETFILE + CollabService.DELIMITER + file;
    }
    
}
